package org.jeecg.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.modules.zcgl.entity.AssetInfo;
import org.jeecg.modules.zcgl.entity.ReceivablesInfo;
import org.jeecg.modules.zcgl.enums.PayStatusEnum;
import org.jeecg.modules.zcgl.enums.YesNoEnum;
import org.jeecg.modules.zcgl.service.IAssetInfoService;
import org.jeecg.modules.zcgl.service.IReceivablesInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author scott
 * @since 2018-12-17
 */
@RestController
@RequestMapping("/index")
//@Api(tags="首页")
@Slf4j
public class IndexController {

    @Autowired
    private IAssetInfoService assetInfoService;
    @Autowired
    private ISysDictService sysDictService;

    @Autowired
    private IReceivablesInfoService receivablesInfoService;


    /**
     * 获取资产出租情况
     *
     * @return
     */
    @GetMapping(value = "/getAssetInfoStatus")
    public Result<List<Map<String, Object>>> getAssetInfoStatus() {
        QueryWrapper<AssetInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("COUNT(id) total, `status` status");
        queryWrapper.lambda().groupBy(AssetInfo::getStatus);
        List<Map<String, Object>> maps = assetInfoService.listMaps(queryWrapper);
        List<DictModel> dictModels = sysDictService.queryDictItemsByCode("zcgl_asset_status");

        if (maps != null && dictModels != null) {
            maps.forEach(map -> {
                for (DictModel dictModel : dictModels) {
                    if (map.get("status") == null) {
                        map.put("name", "未知");
                    } else if (dictModel.getValue().equalsIgnoreCase(map.get("status").toString())) {
                        map.put("name", dictModel.getText());
                        dictModels.remove(dictModel);
                        break;
                    }
                }
            });
            if (dictModels.size() > 0) {
                dictModels.forEach(dictModel -> {
                    Map<String, Object> map1 = new HashMap<>();
                    map1.put("name", dictModel.getText());
                    map1.put("status", dictModel.getValue());
                    map1.put("total", 0);
                    maps.add(map1);
                });
            }
        }
        return Result.ok(maps);
    }

    /**
     * 获取缴费情况
     *
     * @param type      1今日 2本周 3本月 4本年 5选择日期
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping(value = "/getReceivablesInfoPayStatus")
    public Result<List<Map<String, Object>>> getReceivablesInfoPayStatus(
            @RequestParam(name = "type", defaultValue = "3") Integer type,
            @RequestParam(name = "startDate", required = false) String startDate, @RequestParam(name = "endDate", required = false) String endDate) {

        List<Integer> statusList = new ArrayList<>();
        statusList.add(PayStatusEnum.PAID.getStatus());
        statusList.add(PayStatusEnum.UNPAID.getStatus());
        QueryWrapper<ReceivablesInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("SUM(amount_money) total, pay_status payStatus");
        queryWrapper.lambda().in(ReceivablesInfo::getPayStatus, statusList); //查询待付款和已付款的
        queryWrapper.lambda().eq(ReceivablesInfo::getAmountConfirmation, YesNoEnum.YES.getStatus()); //金额已确认的
        queryWrapper.lambda().groupBy(ReceivablesInfo::getPayStatus);

        if (type == 1) {
            queryWrapper.lambda().eq(ReceivablesInfo::getPayStartDate, DateUtils.getDate(DateUtils.DATE_FORMAT));
        } else if (type == 2) {
            queryWrapper.lambda().between(ReceivablesInfo::getPayStartDate, DateUtils.getCurrentMonday(), DateUtils.getPreviousSunday());
        } else if (type == 3) {
            queryWrapper.lambda().between(ReceivablesInfo::getPayStartDate, DateUtils.getMinMonthDate(), DateUtils.getMaxMonthDate());
        } else if (type == 4) {
            queryWrapper.lambda().between(ReceivablesInfo::getPayStartDate, DateUtils.getCurrYearFirst(), DateUtils.getCurrYearLast());
        } else {
            /*if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
                queryWrapper.lambda().between(ReceivablesInfo::getPayStartDate, startDate, endDate);
            } else */if (StringUtils.isNotBlank(startDate)){
                queryWrapper.lambda().ge(ReceivablesInfo::getPayStartDate, startDate);
            }/* else */if (StringUtils.isNotBlank(endDate)){
                queryWrapper.lambda().le(ReceivablesInfo::getPayStartDate, endDate);
            }
        }

        List<Map<String, Object>> receivablesInfoMaps = receivablesInfoService.listMaps(queryWrapper);

        if (receivablesInfoMaps != null) {
            receivablesInfoMaps.forEach(map -> {
                map.put("name", PayStatusEnum.getName(Integer.valueOf(map.get("payStatus").toString())));
                statusList.remove(map.get("payStatus"));
            });
            if (statusList.size() > 0) {
                statusList.forEach(status -> {
                    Map<String, Object> map1 = new HashMap<>();
                    map1.put("name", PayStatusEnum.getName(status));
                    map1.put("status", status);
                    map1.put("total", 0);
                    receivablesInfoMaps.add(map1);
                });
            }
        }
        return Result.ok(receivablesInfoMaps);
    }


}